package com.cj.controller;

import java.util.List;

import com.cj.model.Employee;
import com.cj.model.Post;
import com.cj.model.PostSalary;
import com.jfinal.core.Controller;

public class PostSalaryController extends Controller {
	
	public void index() {
		List<PostSalary> postSalaries = PostSalary.me.getAllPostSalary();
		for (PostSalary postSalary : postSalaries) {
			postSalary.put("insuranceRate", getInsuranceRate(postSalary));
		}
		setAttr("postSalaries", postSalaries);
		render("postSalary.jsp");
	}
	
	private double getInsuranceRate(PostSalary postSalary) {
		if (postSalary == null) return 0d;
		return postSalary.getDouble("medicareRate") + postSalary.getDouble("endowmentRate") + postSalary.getDouble("birthRate")
				+ postSalary.getDouble("injuryRate") + postSalary.getDouble("unemploymentRate") + postSalary.getDouble("housingRate");
	}
	
	public void myStructure() {
		Employee employee = (Employee)getSession().getAttribute("employee");
		Integer postId = employee.getInt("postId");
		PostSalary postSalary = PostSalary.me.getPostSalaryByPost(postId);
		setAttr("postsalary", postSalary);
		render("mySalaryStructure.jsp");
	}
	
	public void addPre() {
		List<PostSalary> postSalaries = PostSalary.me.getAllPostSalary();
		List<Post> posts = Post.me.getAllPosts();
		if (posts != null && !posts.isEmpty()) {
			for (int i = posts.size()-1; i>0; i--) {
				Post post = posts.get(i);
				Integer postId = post.getInt("postId");
				for (PostSalary postSalary : postSalaries) {
					if(postId == postSalary.getInt("postId")) {
						posts.remove(post);
						break;
					};
				}
			}
		}
		setAttr("posts", posts);
		render("SalaryStructureAddPre.jsp");
	}
	
	public void editPre() {
		List<Post> posts = Post.me.getAllPosts();
		setAttr("posts", posts);
		Integer postSalaryId = getParaToInt("postSalaryId");
		PostSalary postSalary = PostSalary.me.findById(postSalaryId);
		setAttr("postsalary", postSalary);
		render("SalaryStructureEditPre.jsp");
	}
	
	public void add() {
		PostSalary postSalary = new PostSalary();
		setPostSalary(postSalary);
		if(postSalary.save()) {
			redirect("/postSalary");
		}
	}
	
	public void edit() {
		PostSalary postSalary = getModel(PostSalary.class);
		setPostSalary(postSalary);
		if(postSalary.update()) {
			redirect("/postSalary");
		}
	}
	
	private void setPostSalary(PostSalary postSalary) {
		Double housingRate = Double.parseDouble(getPara("postsalary.housingRate"));
		Double leaveRate = Double.parseDouble(getPara("postsalary.leaveRate"));
		Double unemploymentRate = Double.parseDouble(getPara("postsalary.unemploymentRate"));
		Double basicSalary = Double.parseDouble(getPara("postsalary.basicSalary"));
		Double birthRate = Double.parseDouble(getPara("postsalary.birthRate"));
		Double postId = Double.parseDouble(getPara("postsalary.postId"));
		Double endowmentRate = Double.parseDouble(getPara("postsalary.endowmentRate"));
		Double extraWorkRate = Double.parseDouble(getPara("postsalary.extraWorkRate"));
		Double neglectWorkRate = Double.parseDouble(getPara("postsalary.neglectWorkRate"));
		Double lateRate = Double.parseDouble(getPara("postsalary.lateRate"));
		Double medicareRate = Double.parseDouble(getPara("postsalary.medicareRate"));
		Double travelRate = Double.parseDouble(getPara("postsalary.travelRate"));
		Double injuryRate = Double.parseDouble(getPara("postsalary.injuryRate"));
		postSalary.set("basicSalary", basicSalary);
		postSalary.set("postId", postId);
		if(housingRate != null) {
			postSalary.set("housingRate", -housingRate);
		} else {
			postSalary.set("housingRate", 0);
		}
		if(leaveRate != null) {
			postSalary.set("leaveRate", -leaveRate);
		} else {
			postSalary.set("leaveRate", 0);
		}
		if(unemploymentRate != null) {
			postSalary.set("unemploymentRate", -unemploymentRate);
		} else {
			postSalary.set("unemploymentRate", 0);
		}
		if(birthRate != null) {
			postSalary.set("birthRate", -birthRate);
		} else {
			postSalary.set("birthRate", 0);
		}
		if(endowmentRate != null) {
			postSalary.set("endowmentRate", -endowmentRate);
		} else {
			postSalary.set("endowmentRate", 0);
		}
		if(extraWorkRate != null) {
			postSalary.set("extraWorkRate", extraWorkRate);
		} else {
			postSalary.set("extraWorkRate", 0);
		}
		if(neglectWorkRate != null) {
			postSalary.set("neglectWorkRate", -neglectWorkRate);
		} else {
			postSalary.set("neglectWorkRate", 0);
		}
		if(lateRate != null) {
			postSalary.set("lateRate", -lateRate);
		} else {
			postSalary.set("lateRate", 0);
		}
		if(medicareRate != null) {
			postSalary.set("medicareRate", -medicareRate);
		} else {
			postSalary.set("medicareRate", 0);
		}
		if(travelRate != null) {
			postSalary.set("travelRate", -travelRate);
		} else {
			postSalary.set("travelRate", 0);
		}
		if(injuryRate != null) {
			postSalary.set("injuryRate", -injuryRate);
		} else {
			postSalary.set("injuryRate", 0);
		}
	}
	
	public void delete() {
		Integer postSalaryId = getParaToInt("postSalaryId");
		if(PostSalary.me.deleteById(postSalaryId)) {
			renderJson("flag", true);
		} else {
			renderJson("flag", false);
		}
	}
	
	
}
